﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MergeUi : BaseUi
{
    public Transform grid;
    public UserInput userInput;
    private MergeCellUi[] cells;
    private int dragingIndex = -1;
    private int greenIndex = -1;
    void Start()
    {
        cells = new MergeCellUi[Model.mergeGrid.Length];
        for (int i = 0; i < Model.mergeGrid.Length; i++)
        {
            MergeCellUi cell = loadUi<MergeCellUi>("merge", "MergeCellUi", grid);
            cell.index = i;
            cells[i] = cell;
        }
        userInput.touchBegan = new UserInputTouchBegin(onTouchBegan);
        userInput.touchMoved = new UserInputTouchMoved(onTouchMoved);
        userInput.touchEnded = new UserInputTouchEnded(onTouchEnded);
    }
    private int getTouchIndex(Vector3 pos)
    {
        Vector3 v = grid.InverseTransformPoint(pos);
        Rect gridRect = grid.GetComponent<RectTransform>().rect;
        // 转为以左上角为原点的坐标系
        float x = v.x + gridRect.width / 2;
        float y = -v.y + gridRect.height / 2;
        if (x >= 0 && x <= gridRect.width && y >= 0 && y <= gridRect.height)
        {
            int index = (int)Mathf.Floor(x / (gridRect.width / 4)) + (int)Mathf.Floor(y / (gridRect.height / 3)) * 4;
            return index;
        }
        else
        {
            return -1;
        }
    }
    void onTouchBegan(Vector3 pos)
    {
        int index = getTouchIndex(pos);
        if (index != -1)
        {
            dragingIndex = index;
        }
    }
    void onTouchMoved(Vector3 pos)
    {
        if (dragingIndex == -1) {
            return;
        }
        Vector3 pos2 = cells[dragingIndex].transform.InverseTransformPoint(pos);
        RectTransform rt = cells[dragingIndex].objTransform.GetComponent<RectTransform>();
        float halfWidth = rt.rect.width/2;
        float halfHeight = rt.rect.height/2;
        rt.offsetMin = new Vector2(pos2.x - halfWidth, pos2.y - halfHeight);
        rt.offsetMax = new Vector2(pos2.x + halfWidth, pos2.y + halfHeight);
        int index = getTouchIndex(pos);
        if (greenIndex != -1 && greenIndex != index) 
        {
            cells[greenIndex].objImage.color = Color.white;
        }
        if (index != dragingIndex && index != -1) {
            greenIndex = index;
            cells[greenIndex].objImage.color = Color.green;
        }
    }
    void onTouchEnded(Vector3 pos)
    {
        // Debug.Log("onTouchEnded");
        if (dragingIndex == -1) {
            return;
        }
        if (greenIndex != -1) {
            Model.mergeGrid[greenIndex] ++;
            Model.mergeGrid[dragingIndex] = 0;
            cells[greenIndex].refreshObj();
        }
        cells[dragingIndex].refreshObj();
    }
    public void buyBtnClick()
    {
        for (int i = 0; i < Model.mergeGrid.Length; i++)
        {
            if (Model.mergeGrid[i] == 0)
            {
                Model.mergeGrid[i] = 1;
                cells[i].refreshObj();
                break;
            }
        }
    }
    public void Update()
    {

    }
}
